package com.test.travelinformation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.travelinformation.R;
import com.test.travelinformation.entity.User;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 用户名
     */
    private EditText mEditLoginName;
    /**
     * 密码
     */
    private EditText mEditLoginPassword;
    /**
     * 确认密码
     */
    private EditText mEditLoginPasswordTwo;
    /**
     * 注册
     */
    private Button mBtnSign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        initView();
    }

    private void initView() {
        mEditLoginName = (EditText) findViewById(R.id.edit_login_name);
        mEditLoginPassword = (EditText) findViewById(R.id.edit_login_password);
        mEditLoginPasswordTwo = (EditText) findViewById(R.id.edit_login_password_two);
        mBtnSign = (Button) findViewById(R.id.btn_sign);
        mBtnSign.setOnClickListener(this);
    }

    private void sign(){
        String name = mEditLoginName.getText().toString();
        String password = mEditLoginPassword.getText().toString();
        String passwordTwo = mEditLoginPasswordTwo.getText().toString();
        if ("".equals(name) || TextUtils.isEmpty(name)){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            mEditLoginName.setFocusable(true);
            mEditLoginName.setFocusableInTouchMode(true);
            mEditLoginName.requestFocus();
        }else if ("".equals(password) || TextUtils.isEmpty(password)){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            mEditLoginPassword.setFocusable(true);
            mEditLoginPassword.setFocusableInTouchMode(true);
            mEditLoginPassword.requestFocus();
        }else if ("".equals(passwordTwo) || TextUtils.isEmpty(passwordTwo)){
            Toast.makeText(this,"确认密码不能为空",Toast.LENGTH_SHORT).show();
            mEditLoginPasswordTwo.setFocusable(true);
            mEditLoginPasswordTwo.setFocusableInTouchMode(true);
            mEditLoginPasswordTwo.requestFocus();
        }else if (!password.equals(passwordTwo)){
            Toast.makeText(this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
        }else {
            User user = new User();
            user.setUsername(name);
            user.setPassword(password);
            final User signUser = user;
            user.login(new SaveListener<User>() {
                @Override
                public void done(User user, BmobException e) {
                    if (e == null){
                        Toast.makeText(SignActivity.this,"该用户已被注册",Toast.LENGTH_SHORT).show();
                        mEditLoginName.setFocusable(true);
                        mEditLoginName.setFocusableInTouchMode(true);
                        mEditLoginName.requestFocus();
                    }else {
                        signUser.signUp(new SaveListener<User>() {
                            @Override
                            public void done(User o, BmobException e) {
                                if (e == null){
                                    Toast.makeText(SignActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(SignActivity.this,LoginActivity.class));
                                    SignActivity.this.finish();
                                }else {
                                    Toast.makeText(SignActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            });

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_sign:
                sign();
                break;
        }
    }
}
